<?php
class LoaicdcbsController extends AppController {

	var $name = 'Loaicdcbs';

	function index() {
		$this->Loaicdcb->recursive = 0;
		$this->set('loaicdcbs', $this->paginate());
	}
    function phanloaicd(){
        $namhoc_id=$this->Session->read('namhocht_id');
        $chidoan_id=$this->Session->read('chidoanht_id');
        $check=$this->Loaicdcb->find('first',array('conditions'=>array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$chidoan_id),'recursive'=>-1));
        //pr($this->data);exit;
        $loaicd=$this->data['Loaicdcb']['xeploaicd_id'];
        if($check==null){
            $this->Loaicdcb->saveAll(array('xeploaicd_id'=>$loaicd,'namhoc_id'=>$namhoc_id,'chidoansv_id'=>$chidoan_id));    
        }else $this->Loaicdcb->updateAll(array('xeploaicd_id'=>$loaicd),array('namhoc_id'=>$namhoc_id,'chidoansv_id'=>$chidoan_id));
        $this->redirect(array('controller'=>'phanloaicbs','action' => 'index'));
    }
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid loaicdcb', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('loaicdcb', $this->Loaicdcb->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Loaicdcb->create();
			if ($this->Loaicdcb->save($this->data)) {
				$this->Session->setFlash(__('The loaicdcb has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The loaicdcb could not be saved. Please, try again.', true));
			}
		}
		$chidoansvs = $this->Loaicdcb->Chidoansv->find('list');
		$xeploais = $this->Loaicdcb->Xeploai->find('list');
		$namhocs = $this->Loaicdcb->Namhoc->find('list');
		$this->set(compact('chidoansvs', 'xeploais', 'namhocs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid loaicdcb', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Loaicdcb->save($this->data)) {
				$this->Session->setFlash(__('The loaicdcb has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The loaicdcb could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Loaicdcb->read(null, $id);
		}
		$chidoansvs = $this->Loaicdcb->Chidoansv->find('list');
		$xeploais = $this->Loaicdcb->Xeploai->find('list');
		$namhocs = $this->Loaicdcb->Namhoc->find('list');
		$this->set(compact('chidoansvs', 'xeploais', 'namhocs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for loaicdcb', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Loaicdcb->delete($id)) {
			$this->Session->setFlash(__('Loaicdcb deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Loaicdcb was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
